#include "public.h"

string getStyleTime(time_t t)
{
    char styletime[64];
    tm *st_time = localtime(&t);
    sprintf(styletime, "%d-%02d-%02d %02d:%02d:%02d", st_time->tm_year + 1900, st_time->tm_mon + 1, st_time->tm_mday, st_time->tm_hour, st_time->tm_min, st_time->tm_sec);
    return string(styletime);
}

void getFFmpegError(int errnum)
{
    char errbuf[128];
    av_strerror(errnum, errbuf, sizeof(errbuf));
    log_printf(LOG_LEV, "%s %d", errbuf, errnum);
}

string getFFmpegErrroStr(int errnum)
{
    char errbuf[128];
    av_strerror(errnum, errbuf, sizeof(errbuf));
    string str(errbuf);
    return str;
}

tm *getCurDatetime()
{
    time_t cur = time(NULL);
    return localtime(&cur);
}

string getStyleTime()
{
    time_t cur = time(NULL);
    return getStyleTime(cur);
}

string getStyleTimeMS()
{
    struct timeval now;
    gettimeofday(&now, nullptr);
    int ms = now.tv_usec / 1000;

    char dateTime[32] = {0};
    sprintf(dateTime, "%s:%d", getStyleTime().c_str(), ms);
    return dateTime;
}


YUYV422FileWriter::YUYV422FileWriter(string path, int maxSize)
{
    m_file = fopen(path.c_str(), "w+");
    m_maxSize = maxSize;
    m_totalSize = 0;
}

YUYV422FileWriter::~YUYV422FileWriter()
{
    if(m_file)
    {
        fclose(m_file);
        m_file = nullptr;
    }
}

int YUYV422FileWriter::writeYUYV422Data(const char *data, unsigned int size)
{
    //判断大小 -1表示无限制
    if(m_maxSize != -1)
    {
        if(m_totalSize > m_maxSize)
        {
            if(m_file)
            {
                fclose(m_file);
                m_file = nullptr;
            }

            return -1;
        }
    }

    if(!m_file || data == nullptr || size <= 0)
    {
        return -1;
    }

    fwrite(data, 1, size, m_file);
    m_totalSize += size;

    return 0;
}


YUV420PFileWriter::YUV420PFileWriter(string path, int maxSize)
{
    m_file = fopen(path.c_str(), "w+");
    m_maxSize = maxSize;
    m_totalSize = 0;
}

YUV420PFileWriter::~YUV420PFileWriter()
{
    if(m_file)
    {
        fclose(m_file);
        m_file = nullptr;
    }
}

int YUV420PFileWriter::writeYUV420PData(AVFrame *frame)
{
    //判断大小 -1表示无限制
    if(m_maxSize != -1)
    {
        if(m_totalSize > m_maxSize)
        {
            if(m_file)
            {
                fclose(m_file);
                m_file = nullptr;
            }

            return -1;
        }
    }

    if(!m_file || frame == nullptr)
    {
        return -1;
    }

    int size = frame->width * frame->height;
    if(1)
    {
        //方式1
        fwrite(frame->data[0], 1, size, m_file);
        fwrite(frame->data[1], 1, size/4, m_file);
        fwrite(frame->data[0], 1, size/4, m_file);
    }
    else
    {
        //方式2
        for(int i=0; i<frame->height; i++)
        {
            fwrite(frame->data[0], 1, frame->linesize[0], m_file);
            frame->data[0] += frame->linesize[0];
        }
        for(int i=0; i<frame->height/2; i++)
        {
            fwrite(frame->data[1], 1, frame->linesize[1]/2, m_file);
            frame->data[1] += frame->linesize[1];
        }
        for(int i=0; i<frame->height/2; i++)
        {
            fwrite(frame->data[2], 1, frame->linesize[2]/2, m_file);
            frame->data[2] += frame->linesize[2];
        }
    }

    m_totalSize += size * 3 / 2;
    return 0;
}
